/*
 * To change this license header, choose License Headers in Project Properties.
 * To change this template file, choose Tools | Templates
 * and open the template in the editor.
 */
package pgentity;

import db.DBContext;
import db.PGKeys;
import db.RedisKey;
import pgentity.pool.EntityFactory;
import pgentity.pool.PooledEntity;
import share.PGHelper;
import share.TimeUtil;

/**
 *
 * @author Salm
 */
@EntityFactory(factorier = "loadEntity")
public class Snowman implements PooledEntity {
    private final String uid;
    private final RedisKey redisKey;

    private Snowman(String uid) {
        this.uid = uid;
        this.redisKey = redisKey(uid);
    }
    
    public static RedisKey redisKey(String uid)
    {
        return PGKeys.USERS.getChild(uid)
                .getChild(PGKeys.FD_SNOWMAN);
    }
    
    public static Snowman getEntity(String uid)
    {
        return loadEntity(uid);
//        return EntityPool.inst().get(Snowman.class, uid);
    }
    
    public static Snowman loadEntity(String uid)
    {
        Snowman snowman = new Snowman(uid);
        return snowman;
    }
    
    public int currentStep()
    {
        return PGHelper.toInt(DBContext.Redis().get(redisKey));
    }
    
    public int build()
    {
        return DBContext.Redis().incrENX(redisKey, TimeUtil.DAY_SECS).intValue();
    }
    
    public void reset()
    {
        DBContext.Redis().del(redisKey);
    }
}
